"use strict";

var app = require('koa')();
var bodyParser = require('koa-better-body');

// static image
var serve = require('koa-static');
app.use(serve(__dirname + '/static/'));

// bodyparser
app.use(bodyParser({
  multipart: true,
  formidable: {
    uploadDir: __dirname + '/uploads'
  }
}));

var router = require('koa-router')();

router.post('/test/json', function*() {
  console.log('ok');
  console.log('fields:', this.request.body.fields);
  console.log('files:', this.request.body.files);
  this.body = 'json ok';
});
router.post('/test/file', function*() {
  console.log('ok');
  console.log('fields:', this.request.body.fields);
  console.log('files:', this.request.body.files);
  this.body = 'file ok';
});
app.use(router.routes());


// 404
app.use(function*() {
  this.status = 404;
  this.body = '~~~404 not found~~~';
});


var port = 882;
app.listen(port, '0.0.0.0', function () {
  console.log('server listen at ' + port);
});